#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   models.py
@Time    :   2020/09/22 10:14:49
@Author  :   fengzhilaoling 
@Version :   1.0
@Contact :   fengzhilaoling@163.com
'''
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash
from werkzeug.security import check_password_hash
import os
import sys

cur_dir = os.path.dirname(__file__)
sys.path.append(cur_dir)
from exts import db

print(cur_dir)


# db = SQLAlchemy()

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    u_name = db.Column(db.String(10), unique=True)
    password = db.Column(db.String(128))

    def __init__(self, id, u_name, password):
        self.id = id
        self.u_name = u_name
        self.password = self.set_password(password)

    # 将密码进行加密
    def set_password(self, password):
        return generate_password_hash(password)

    # 检测密码。传入的是明文密码，会对其进行加密，来进行比较
    def check_password(self, password):
        return check_password_hash(self.password, password)

    # 修改密码
    def update_password(self, password):
        self.password = self.set_password(password)
